//
//  YCShopProductCollectionViewCell.m
//  YCSchool
//
//  Created by Yanglixia on 16/4/30.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  商品cell

#import "YCShopProductCollectionViewCell.h"

@interface YCShopProductCollectionViewCell()

//@property (nonatomic, strong) UIView *bg;//背景
//@property (nonatomic, strong) UIImageView *bgView;//背景图片
//@property (nonatomic, strong) UILabel *foodNameLabel;// 商品名字
//@property (nonatomic, strong) UILabel *priceLabel;// 价格
//@property (nonatomic, strong) UILabel *numberLabel;//数量
//@property (nonatomic, strong) UIButton *reduceBtn;//减价
//@property (nonatomic, strong) UILabel *untiLabel;//
//@property (nonatomic, strong) UIButton *plusBtn;//+
@property (nonatomic, readonly) CGFloat fontSize;//大小

@end

@implementation YCShopProductCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [YCAdapter performOtherBlock:^{
            _fontSize = 15;
        } IPhone6Block:^{
            _fontSize = 16.5;
        } IPhone6pBlock:^{
            _fontSize = 17.5;
        }];
        
        _bg = [[UIView alloc] init];
        _bg.backgroundColor = [UIColor whiteColor];
        _bg.layer.cornerRadius = 6;
        _bg.clipsToBounds = YES;
        [self.contentView addSubview:_bg];
        
        _bgView = [[UIImageView alloc]init];
        _bgView.clipsToBounds = NO;
        _bgView.contentMode = UIViewContentModeScaleAspectFill;
        _bgView.layer.masksToBounds = YES;
        _bgView.userInteractionEnabled = YES;
        [self.bg addSubview:_bgView];
        
        _foodNameLabel = [[UILabel alloc] init];
        _foodNameLabel.font = [UIFont systemFontOfSize:_fontSize];
        _foodNameLabel.textColor = [UIColor blackColor];
        [self.bg addSubview:self.foodNameLabel];
        
        _untiLabel = [[UILabel alloc] init];
        _untiLabel.font = [UIFont systemFontOfSize:12];
        _untiLabel.textColor = [UIColor blackColor];
        [self.bg addSubview:self.untiLabel];
        
        
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont systemFontOfSize:_fontSize];
        _priceLabel.textColor = [UIColor redColor];
        [self.bg addSubview:_priceLabel];
        
        _reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_reduceBtn setImage:[UIImage imageNamed:@"commodity_reduct"] forState:UIControlStateNormal];
        [_reduceBtn addTarget:self action:@selector(didTouchUpInsideAddAndReduceButton:) forControlEvents:UIControlEventTouchUpInside];
        _reduceBtn.imageView.contentMode = UIViewContentModeCenter;
        _reduceBtn.hidden = YES;
        [self.bg addSubview:_reduceBtn];
        
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.textColor = [UIColor grayColor];
        _numberLabel.font = [UIFont systemFontOfSize:_fontSize];
        _numberLabel.textAlignment = NSTextAlignmentCenter;
        _numberLabel.hidden = YES;
        [self.bg addSubview:_numberLabel];
        
        
        _plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_plusBtn setImage:[UIImage imageNamed:@"commodity_add"] forState:UIControlStateNormal];
        [_plusBtn addTarget:self action:@selector(didTouchUpInsideAddAndReduceButton:) forControlEvents:UIControlEventTouchUpInside];
        _plusBtn.imageView.contentMode = UIViewContentModeCenter;
        [self.bg addSubview:_plusBtn];
        
        
        [_bg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.and.left.right.equalTo(self.contentView);
        }];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.left.right.equalTo(self.bg);
            make.bottom.equalTo(self.bg).offset(-60);
        }];
        
        [_foodNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bg).offset(5);
            make.top.equalTo(_bgView.mas_bottom).offset(5);
            make.right.equalTo(_bg);
            
        }];
        
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bg).offset(5);
            make.top.equalTo(_foodNameLabel.mas_bottom).offset(5);
        }];
        
        [_untiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_priceLabel);
            make.left.equalTo(_priceLabel.mas_right).offset(4);
        }];
        
        [_plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-10);
            make.centerY.equalTo(_priceLabel);
            make.height.equalTo(@50);
        }];
        
        [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_plusBtn.mas_left).with.offset(-5);
            make.centerY.equalTo(_priceLabel);
        }];
        
        [_reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_numberLabel.mas_left).with.offset(-5);
            make.centerY.equalTo(_priceLabel);
            make.height.equalTo(@50);
        }];
        
    }
    return self;
}

- (void)didTouchUpInsideAddAndReduceButton:(UIButton *)button
{
    if([_delegate respondsToSelector:@selector(shopProductCell:didTouchUpInsideReduceAndReduceButton:)])
    {
        [self.delegate shopProductCell:self didTouchUpInsideReduceAndReduceButton:button];
    }
}



@end
